<template>
  <el-dialog
    top="10vh"
    width="700px"
    title="添加/编辑"
    :visible.sync="dialogValue"
    :close-on-click-modal="false"
    append-to-body
  >
    <SubmitForm
      :rule-form="ruleForm"
      :rules="rules"
      :form-list="formList"
      @change="handleFormChange"
      @save="onSave"
      @close="dialogValue = false"
    />

  </el-dialog>
</template>

<script>
import { http } from '@/api'
export default {
  name: 'FormAdd',
  data() {
    return {
      apiKey: 'house',
      dialogValue: false,
      ruleForm: {
      },
      defaultForm: {
        community_id: '',
        building_id: '',
        unit_number: '',
        floor_number: '',
        house_number: '',
        owner_id: '',
        building_area: '',
        living_area: '',
      },
      rules: {
        community_id: [{ required: true, message: '请选择所属小区', trigger: 'change' }],
        building_id: [{ required: true, message: '请选择所属楼栋', trigger: 'change' }],
        unit_number: [{ required: true, message: '请输入单元号', trigger: 'blur' }],
        floor_number: [{ required: true, message: '请输入楼层号', trigger: 'blur' }],
        house_number: [{ required: true, message: '请输入房号', trigger: 'blur' }],
        building_area: [{ required: true, message: '请输入建筑面积', trigger: 'blur' }],
        living_area: [{ required: true, message: '请输入套内面积', trigger: 'blur' }],
        total_households: [{ required: true, message: '请输入总户数', trigger: 'blur' }],
        owner_id: [{ required: true, message: '请选择业主', trigger: 'change' }]
      },
      formList: [
        {
          key: 'community_id',
          label: '所属小区',
          name: 'select',
          options: [],
          colSpan: 12,
        },
        {
          key: 'building_id',
          label: '所属楼栋',
          name: 'select',
          options: [],
          colSpan: 12,
        },
        {
          key: 'unit_number',
          label: '单元号',
          name: 'input',
          colSpan: 12,
        },
        {
          key: 'floor_number',
          label: '楼层号',
          name: 'input',
          colSpan: 12,
        },
        {
          key: 'house_number',
          label: '房号',
          name: 'input',
          colSpan: 12,
        },
        {
          key: 'building_area',
          label: '建筑面积',
          name: 'input',
          colSpan: 12,
        },
        {
          key: 'living_area',
          label: '套内面积',
          name: 'input',
          colSpan: 12,
        },
        {
          key: 'total_households',
          label: '总户数',
          name: 'input',
          colSpan: 12,
        },
        {
          key: 'owner_id',
          label: '业主',
          name: 'select',
          options: [],
          colSpan: 12,
        }
      ]
    }
  },
  methods: {
    handleFormChange({ key, val }) {
      if (key === 'community_id') {
        this.formList[1].options = []
        if (val) {
          http.building.list({ community_id: val }).then(res => {
            this.formList[1].options = res.data.map(item => {
              return {
                name: item.name,
                value: item.id
              }
            })
          })
        }
      }
    },
    open(row) {
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data
          }
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm
        }
      }
      http.community.list().then(res => {
        this.formList[0].options = res.data.map(item => {
          return {
            name: item.name,
            value: item.id
          }
        })
      })
      http.owner.list().then(res => {
        this.formList[8].options = res.data.map(item => {
          return {
            name: item.name,
            value: item.id
          }
        })
      })
    },
    onSave() {
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, { ...this.ruleForm }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
